package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.Advertising;
import org.dromara.business.domain.bo.AdvertisingBo;
import org.dromara.business.domain.vo.AdvertisingVo;
import org.dromara.business.service.AdvertisingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

/**
 * 广告
 *
 * @author zlj
 * @date 2024-12-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/advertising")
public class AdvertisingController extends BaseController {

    private final AdvertisingService advertisingService;

    /**
     * 查询广告列表
     */
//    @SaCheckPermission("system:advertising:list")
    @GetMapping("/list")
    public TableDataInfo<AdvertisingVo> list(AdvertisingBo bo, PageQuery pageQuery) {
        return advertisingService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询广告审批列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/approveList")
    public TableDataInfo<AdvertisingVo> approveList(AdvertisingBo bo, PageQuery pageQuery) {
        return advertisingService.approveList(bo, pageQuery);
    }

    /**
     * 导出广告列表
     */
//    @SaCheckPermission("system:advertising:export")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AdvertisingBo bo, HttpServletResponse response) {
        List<AdvertisingVo> list = advertisingService.queryList(bo);
        ExcelUtil.exportExcel(list, "广告", AdvertisingVo.class, response);
    }

    /**
     * 获取广告详细信息
     *
     * @param advertisingId 主键
     */
//    @SaCheckPermission("system:advertising:query")
    @GetMapping("/{advertisingId}")
    public R<AdvertisingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long advertisingId) {
        return R.ok(advertisingService.queryById(advertisingId));
    }

    /**
     * 新增广告
     */
//    @SaCheckPermission("system:advertising:add")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdvertisingBo bo) {

        return toAjax( advertisingService.insertByBo(bo));
    }

    /**
     * 修改广告
     */
//    @SaCheckPermission("system:advertising:edit")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdvertisingBo bo) {
        return toAjax(advertisingService.updateByBo(bo));
    }

    /**
     * 广告审核
     * @param bo
     * @return
     */
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/examineAdvertising")
    public R<Void> examineAdvertising(@Validated(EditGroup.class) @RequestBody AdvertisingBo bo) {
        return toAjax(advertisingService.examineAdvertising(bo));
    }



    /**
     * 删除广告
     *
     * @param advertisingIds 主键串
     */
//    @SaCheckPermission("system:advertising:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{advertisingIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] advertisingIds) {
        return toAjax(advertisingService.deleteWithValidByIds(List.of(advertisingIds), true));
    }


    /**
     * 看广告
     * @param city
     * @return
     */
//    @SaCheckPermission("system:advertising:watchAdvertisements")
    @GetMapping("/watchAdvertisements")
    public R<AdvertisingVo> watchAdvertisements(String city) {
        return R.ok(advertisingService.watchAdvertisements(city));
    }

    @GetMapping("/getCountOfRegions")
    public R<Void> getCountOfRegions(String city) {
        Long countOfRegions = advertisingService.getCountOfRegions(city);
        if (countOfRegions <200 ) {

           return R.ok();
        }
        return R.fail(city+"的广告数量达到了200条,不能发布广告");
    }

}
